define(function (require, exports, module) {
    var config;
    window._config;
    $.fn.verify = function() {
        var url = '/Verify' + '?r=' + new Date().toString();
        var fn = $(this);
        if (fn.length > 1) {
            fn.each(function() {
                $(this).attr('src', url);
            });
        } else {
            fn.attr('src', url);
        }
    }
    window.checkEmail = function (email) {
        email = $.trim(email);
        if (!email || email.length <= 0) {
            return '请填写Email！';
        }
        var patten = /^[\w\.-]+(\.\w+)*@(\w)+((\.\w+)+)$/;
        if (!patten.test(email)) {
            return '请输入正确的Email！';
        }
        return true;
    }
    function setFun() {
        window.$.fn.showError = function(msg) {
            var html = '';
            html += '<div class="alert alert-warning alert-dismissible fade in" role="alert">';
            html += '<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>';
            html += '<strong>Error:</strong>';
            html += '<span class="info">' + msg + '</span>';
            html += '</div>';
            var obj = $(this);
            if (msg) {
                $(obj).html(html);
            } else {
                $(obj).html('');
            }
        };
        window.loginOut = function () {
            $.ajax({
                url: '/Ajax/Passport/loginOut?stmp=' + new Date().toString(),
                success: function() {
                    window.location.reload();
                }
            });
        };
        window.showArticleSave = function (title, info, articleId) {
            var html ='<div class="modal fade">';
            html += '<div class="modal-dialog">';
            html += '<div class="modal-content">';
            html += '<div class="modal-header">';
            html += '<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>';
            html += '<h4 class="modal-title">' + title +'</h4>';
            html += '</div>';
            html += '<div class="modal-body">';
            html += '<p>' + info + '&hellip;</p>';
            html += '</div>';
            html += '<div class="modal-footer">';
            html += '<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>';
            html += '<a href="/single/detail_' + articleId + '.html" type="button" class="btn btn-primary">预览</a>';
            html += '</div>';
            html += '</div>';
            html += '</div>';
            html += '</div>';
            $(html).modal();
        };
        window.showComplete = function () {
            var html ='<div class="modal fade">';
            html += '<div class="modal-dialog">';
            html += '<div class="modal-content">';
            html += '<div class="modal-header">';
            html += '<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>';
            html += '<h4 class="modal-title">暂时还不能这样！╮(╯Д╰)</h4>';
            html += '</div>';
            html += '<div class="modal-body">';
            html += '<p>您还没有完善个人信息，马上去<a href="/setting.html">完善</a>&hellip;</p>';
            html += '</div>';
            html += '<div class="modal-footer">';
            html += '<button type="button" class="btn btn-default" data-dismiss="modal">先不了</button>';
            html += '<a href="/setting.html" type="button" class="btn btn-primary">去完善</a>';
            html += '</div>';
            html += '</div>';
            html += '</div>';
            html += '</div>';
            $(html).modal();
        }
        window.showToLogin = function () {
            var html ='<div class="modal fade">';
            html += '<div class="modal-dialog">';
            html += '<div class="modal-content">';
            html += '<div class="modal-header">';
            html += '<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>';
            html += '<h4 class="modal-title">暂时还不能这样！╮(╯Д╰)</h4>';
            html += '</div>';
            html += '<div class="modal-body">';
            html += '<p>您还没有登录！马上<a href="/login.html?fromurl=' + encodeURIComponent(config.Uri) + '">登录</a>&hellip;</p>';
            html += '</div>';
            html += '<div class="modal-footer">';
            html += '<a href="/register.html?fromurl=' + encodeURIComponent(config.Uri) + '" class="btn btn-default">注册</a>';
            html += '<a href="/login.html?fromurl=' + encodeURIComponent(config.Uri) + '" type="button" class="btn btn-primary">登录</a>';
            html += '</div>';
            html += '</div>';
            html += '</div>';
            html += '</div>';
            $(html).modal();
        }
    }

    function setHeight() {
        var hh = $(document).height();
        var th = $('header').height();
        var fh = $('footer').height();
        var h = hh - th - fh - 30;
        $('.content').css({minHeight: h + 'px'});
    }

    function bindEvent() {
        $('.js-article-add').on('click', function() {
            if (!config.Email) {
                showToLogin();
                return false;
            }
            if (!config.Username) {
                showComplete();
                return false;
            }
        });
    }

    function init(c) {
        config = c;
        window._config = config;
        setFun();
        setHeight();
        bindEvent();
    }

    exports.getConfig = function() {
        return config;
    };
    exports.init = init;
});